package com.yotoodesign.dianba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.BaseData;
import com.yotoodesign.dianba.bean.Song;
import com.yotoodesign.dianba.ui.MainActivity;
import com.yotoodesign.dianba.util.Constant;

public class SingerSongAdapter extends BaseAdapter {

	private Context     mContext;
	private ArrayList<BaseData> songList;
	
	private float curX = 0;
	private float curY = 0;
	
	public ArrayList<BaseData> getSongList() {
		return songList;
	}

	public void setSongList(ArrayList<BaseData> songList) {
		this.songList = songList;
	}

	public SingerSongAdapter(Context c){
		this.mContext = c;
	}
	
	@Override
	public int getCount() {
		return songList!=null?songList.size():0;
	}

	@Override
	public Object getItem(int position) {
		return songList!=null?songList.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int cur = position;
		
		View view = LayoutInflater.from(mContext).inflate(R.layout.singer_song_render, null);
		TextView songName = (TextView)view.findViewById(R.id.songName);
		final Song song = (Song)songList.get(position);
		
		songName.setText(song.getName());
		
		//添加歌曲
		final ImageButton btnAddSong = (ImageButton)view.findViewById(R.id.btnAddSong);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				toggleSelect(btnAddSong,song);
			}
		});
		
		view.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				curX = event.getRawX();
				curY = event.getRawY();
				return false;
			}
		});
		
		return view;
	}
	
	private void toggleSelect(ImageButton btnAddSong,Song song){
		btnAddSong.clearAnimation();
		MainActivity activity = (MainActivity)GlobalApplication.activity;
		if(song.isSelected()){
			song.setSelected(false);
			btnAddSong.setBackgroundResource(R.drawable.check_disabled);
			activity.removeSong(song);
		}else{
			song.setSelected(true);
			btnAddSong.setBackgroundResource(R.drawable.check_enabled);
			activity.addSong(song,curX,curY);
		}
	}

}
